﻿@using PearAdminMvcApp.Tools
@model PearAdminMvcApp.Tools.PearTreeTableConfig

@{
    ViewBag.Title = "设置角色权限";
    
}
 <table id="power-table" lay-filter="power-table"></table>


@section footer
{
    <script>
    Array.max = function( array ){ 
    return Math.max.apply( Math, array );
    };
    Array.min = function( array ){ 
    return Math.min.apply( Math, array );
    };
    window.loadedFunc =  function(){
        layui.use( 'dropdown' , function() {
        tools.treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'Id',
            treePidName: 'ParentMenuId',
            skin:'line',
            method:'get',
            treeDefaultClose: true, 
            elem: '#power-table',
            url: '/SysSetting/RoleMenuData/'+@Model.RoleId, 
            toolbar: '#user-toolbar', 
            defaultToolbar:[],
            page: false,
            cols: [
                [ 
                { type:"checkbox"},
                {field: 'MenuText',   title: '权限名称'},
                {field: 'IconString', title: '图标' ,templet:'#icon'},
                {field: 'MenuTypeId', title: '权限类型',templet:'#power-type'} 
                ]
            ] 
           
        });
       
         });
          
          layui.table.on("toolbar(power-table)",res =>{ 
              switch (res.event) {
                 case "save": 
                    var checkStatus = layui.table.checkStatus('power-table'); //idTest 即为基础参数 id 对应的值
                    var items = new Array();
                    console.log(checkStatus.data) //获取选中行的数据
                    console.log(checkStatus.data.length) //获取选中行数量，可作为是否有选中行的条件
                    console.log(checkStatus.isAll ) //表格是否全选
                    console.info();
                    if(checkStatus.data.length === 0){
                        tools.notice.info("没有选中权限")
                        return false;
                    }
                    var f = 0;
                    for(var i of checkStatus.data){
                        items[f] = i.Id;
                        f++;
                    }
                    layui.jquery.ajax({
                        url:"/SysSetting/SetRoleMenu",
                        type:"post",
                        data:{
                            RoleId:@Model.RoleId,
                            MenuId:items
                        }
                    }).done(res => {
                         @Html.ModalOpenDoneFucAndCloseIframeModal("添加成功")
                    })
                     break;
              }
          });
      }
      
    
   </script>
}
<script type="text/html" id="icon">
    <i class="{{d.IconString}}"></i>
</script>
<script type="text/html" id="power-type">
    {{d.MenuTypeId == 1?'Menu':'Page'}}
</script>
 
<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="save"> 
        保存
    </button> 
</script>